/*
 * theoraplayer.h
 *
 *  Created on: 7-ott-2008
 *      Author: fpistonesi
 */

/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

#ifndef THEORAPLAYER_H_
#define THEORAPLAYER_H_

//! Irrlicht
#include "irrlicht.h"

using namespace irr;

#include "theora/theora.h"

enum CTheoraPlayerStates
{
  CTPS_IDLE = 0,
  CTPS_PLAYING,
};

class CTheoraPlayer
{
public:

  CTheoraPlayer(IrrlichtDevice* device);
  ~CTheoraPlayer();

  //! Create
  bool  Create();
  //! Get next frame and update texture
  bool  OnUpdate(u32 timeMs);

  //! Get playing state
  CTheoraPlayerStates GetState() { return iState; }

  //! Get texture interface
  video::ITexture* GetTexture();
  //! Get image interface
  video::IImage*   GetImage();

  //! Start playing
  bool  Play(const c8* fileName, bool loop = true);
  bool  Stop();


private:

  bool  ProcessNextFrame();

  void  UpdateBuffer();
  void  UpdateTexture();

  bool  PrepareOgg();
  bool  PrepareBuffers();

  //! Helper Theora functions
  s32   queue_page(ogg_page *page);
  s32   buffer_data();

  // Irrlicht classes
  IrrlichtDevice*  iIrrDevice;
  video::IVideoDriver* iIrrVideoDriver;
  ILogger*         iIrrLog;

  video::ITexture* iBlankTexture;
  video::ITexture* iTexture;
  video::IImage*   iImage;
  io::IReadFile*   iVideoFile;

  /* Ogg and codec state for demux/decode */
  ogg_sync_state   iOggSyncState;
  ogg_page         iOggPage;
  ogg_stream_state iOggStreamState;
  theora_info      iTheoraInfo;
  theora_comment   iTheoraComment;
  theora_state     iTheoraDecoderState;
  int              iTheoraPacketsCount;
  ogg_packet       iOggPacket;


  bool             iIsLooped;
  u32              iCurrFrame;
  u32              iCurrTime;


  CTheoraPlayerStates   iState;
};

#endif /* THEORAPLAYER_H_ */
